//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CommunityServer.RollerBlogs.Components;
using CommunityServer.Reader.Components;

    namespace CommunityServer.ControlPanel.BlogAdmin
    {
        /// <summary>
        ///		Summary description for BlogFeedsManage.
        /// </summary>
        public class BlogFeedsManage : BaseBlogAdminPage
        {

            #region Child Controls

             protected Literal ExistingMessage;

            #endregion
			protected CommunityServer.ControlPanel.Controls.ControlPanelSelectedNavigation SelectedNavigation1;
			protected CommunityServer.ControlPanel.Controls.ResourceControl SectionDescription;
			protected CommunityServer.Controls.MPContent DescriptionRegion;

			protected CommunityServer.Controls.MPContent TaskRegion;
			protected CommunityServer.Controls.MPContainer Mpcontainer1;
			protected CommunityServer.ControlPanel.Controls.ModalLink NewFeed;
            protected Repeater mirrorFeeds;

            private CSContext context;

			

			private int SectionID
			{
				get
				{
					Object state = ViewState["SectionID"];
					if(state != null)
						return (int)state;
					return -1;
				}
				set { ViewState["SectionID"] = value; }
			}

            override protected void OnInit(EventArgs e)
            {
                this.Load += new EventHandler(this.Page_Load);
				
                base.OnInit(e);
            }

            private void Page_Load(object sender, EventArgs e)
            {
				context = CSContext.Current;
				SectionID = context.GetIntFromQueryString("SectionID", -1);
				NewFeed.Url = string.Format("FeedForm.aspx?SectionID={0}&UrlID={1}",SectionID,"-1");
				JavaScript.RegisterRefresh(this);

				if (context.QueryString.Get("action") == "delete")
					DeleteFeed();

				Bind();
			}

            private ArrayList GetAssignedFeeds()
            {
                ArrayList rbFeeds = RollerBlogFeeds.GetEnabledFeedsBySectionID(SectionID);
                return rbFeeds;
            }

            private void Bind()
            {
               

                ArrayList rbFeeds = GetAssignedFeeds();

                if(rbFeeds.Count > 0)
                {

                    mirrorFeeds.DataSource = rbFeeds;
                    mirrorFeeds.DataBind();
                    ExistingMessage.Text = string.Format(CommunityServer.ControlPanel.Components.ResourceManager.GetString("CP_BlogAdmin_Feeds_Existing"),Weblogs.GetWeblog(this.SectionID).Name);
                    
                }
                else
                {
                    ExistingMessage.Text = string.Format(CommunityServer.ControlPanel.Components.ResourceManager.GetString("CP_BlogAdmin_Feeds_NotExisting"),Weblogs.GetWeblog(this.SectionID).Name);
                    mirrorFeeds.Visible = false;
                }
            }

            private void DeleteFeed()
            {
				int UrlID = context.GetIntFromQueryString("UrlID", -1);
                RollerBlogFeed rbf = new RollerBlogFeed();
                rbf.SectionId = SectionID;
                rbf.UrlId = UrlID;
                RollerBlogFeeds.DeleteFeed(rbf);
            }

           
			private void InitializeComponent()
			{
			
			}
		}
    }

